import { Injectable, ViewChild, EventEmitter } from '@angular/core';
import { GlobalService } from '../../global.service';
import { Menus } from './menus';

@Injectable()

export class MenusService {

    addInfo = new Menus('新增个人信息', 'link', '/add-info');

    indexPage = new Menus('首页', 'link', '/index-page');

    updateInfo = new Menus('个人信息修改', 'link', '/update-info');

    showInfo = new Menus('个人信息展示', 'link', '/show-info');

    practice = new Menus('练习', 'button', '', false, [
        new Menus('题目一', 'link', '/money-convert'),
        new Menus('题目二', 'link', '/mood'),
        new Menus('题目三', 'link', '/stopwatch'),
        new Menus('题目四', 'link', '/map'),
    ]);

    MENUS1 = [this.indexPage, this.updateInfo, this.showInfo, this.practice];

    MENUS2 = [this.addInfo];

    public menusChanged: EventEmitter<any> = new EventEmitter<any>();

    private _curMenus: any;

    public get curMenus(): any {
        return this._curMenus;
    }

    constructor(private globalService: GlobalService) {

        if (this.globalService.isLogin) {
            this._curMenus = this.MENUS1;
        } else {
            this._curMenus = this.MENUS2;
        }

        this.globalService.loginStatusChanged.subscribe(isLogin => {
            if (isLogin) {
                this.menusChanged.emit(this.MENUS1);
                this._curMenus = this.MENUS1;
            } else {
                this.menusChanged.emit(this.MENUS2);
                this._curMenus = this.MENUS2;
            }
        });

    }
}
